﻿#region

using System;
using System.Linq;
using System.Windows;
using NewMixedMode;
using Styx;
using Styx.Logic.BehaviorTree;

#endregion

namespace Ink_Master.Bots.InkMaster.BotHandlers
{
    public class BotSwitching
    {
        /// <summary>
        ///   Changes to ink master.
        /// </summary>
        public static void ChangeToInkMaster()
        {
            Application.Current.Dispatcher.BeginInvoke(
                new Action(() =>
                               {
                                   bool isRunning = TreeRoot.IsRunning;
                                   if (isRunning)
                                   {
                                       TreeRoot.Stop();
                                   }
                                   BotBase bot = MixedModeEx.Instance.SecondaryBot;
                                   if (!bot.Initialized)
                                   {
                                       bot.Initialize();
                                   }

                                   Log.Slog("Changing  to InkMaster");
                                   if (isRunning)
                                   {
                                       TreeRoot.Start();
                                   }
                               }
                    ));
        }

        /// <summary>
        ///   Changes the primary bot.
        /// </summary>
        /// <param name = "botName">Name of the bot.</param>
        public static void ChangePrimaryBot(string botName)
        {
            InkMaster.ChangingPrimaryBot = true;
            Log.Slog("Attempting to Change Primary Bot to: " + botName);
            BotBase bot =
                BotManager.Instance.Bots.Values.FirstOrDefault(
                    b => b.Name.IndexOf(botName, StringComparison.InvariantCultureIgnoreCase) >= 0);
            if (bot != null)
            {
                if (MixedModeEx.Instance.PrimaryBot != null && MixedModeEx.Instance.PrimaryBot.Name != bot.Name ||
                    MixedModeEx.Instance.PrimaryBot == null)
                {
                    MixedModeEx.Instance.PrimaryBot = bot;
                    if (!bot.Initialized)
                    {
                        bot.Initialize();
                    }

                    Log.Slog("Changing PrimaryBot to " + botName);
                }
            }
            else
            {
                Log.Slog("Bot with name: " + botName + " was not found");
            }
        }
    }
}